package com.project.internship.controller.info;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Student;
import com.project.internship.entity.resp.RespBean;
import com.project.internship.entity.resp.RespPageBean;
import com.project.internship.service.StudentService;
import com.project.internship.vo.StudentDeptMajorClassVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学院端学生信息
 */

@RestController
@RequestMapping("/information/student")
public class InfoStudentController {

    @Autowired
    StudentService studentService;

    /**
     * 带关键字(学生学号)的分页查询
     * @param current
     * @param size
     * @param keyword
     * @return
     */
    @GetMapping("/")
    public RespPageBean getAllStudents(@RequestParam(defaultValue = "1") Integer current,
                                       @RequestParam(defaultValue = "10") Integer size,
                                       String keyword){
        Page<Student> page = new Page<>(current,size);

        Long total = studentService.getAllStudents(page,keyword).getTotal();
        List<Student> data =  studentService.getAllStudents(page,keyword).getRecords();

        return new RespPageBean(total,data);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/")
    public RespBean addStudent(@RequestBody Student student){
        if(studentService.addStudent(student) == 1){
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    /**
     * 更新学生信息
     * @param student
     * @return
     */
    @PutMapping("/")
    public RespBean updateStudent(@RequestBody Student student){
        if(studentService.updateStudent(student) == 1){
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    /**
     * 删除一条学生信息
     * @param id 学生表中的主键id
     * @return
     */
    @DeleteMapping("/{id}")
    public RespBean deleteStudent(@PathVariable("id") Long id){
        if(studentService.deleteStudent(id) == 1){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    /**
     * 批量删除学生信息
     * @param ids
     * @return
     */
    @DeleteMapping("/")
    public RespBean deleteStudents(@RequestBody Long[] ids){
        if(studentService.deleteStudents(ids) == ids.length){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }


}
